import fs from "fs/promises";
import { WebPDFLoader } from "@langchain/community/document_loaders/web/pdf";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import { Document } from "@langchain/core/documents";
import { pc } from "@/lib/Pinecone"
import { Md5 } from 'ts-md5'
import { NextResponse } from "next/server";
export async function POST(req: Request) {
  try {
    const formData = await req.formData();
    const file = formData.get('file') as File;

    //1.分割成docs
    const buffer = await file.arrayBuffer()
    const blob = new Blob([buffer], { type: "application/pdf" });

    const loader = new WebPDFLoader(blob, {
      // required params = ...
      // optional params = ...
    });
    const docs = await loader.load();

    //2. spilt docs
    const splitedDocs = await Promise.all(docs.map(doc => splitDoc(doc)))

    //3. 上传到向量库
    const res = await Promise.all(splitedDocs.map(embedChunks))

    return NextResponse.json({ message: 'File uploaded successfully' })
  }
  catch (err) {
    console.log(err)
    return NextResponse.json({ message: 'File uploaded failed' })
  }
}

const splitDoc = async (doc: Document) => {
  const textSplitter = new RecursiveCharacterTextSplitter({
    chunkSize: 1000,  // 从10调整为更合理的1000
    chunkOverlap: 200, // 从1调整为更合理的200
  });

  const texts = await textSplitter.splitText(doc.pageContent);
  return texts;
}

const embedChunks = async (chunks: string[]) => {
  const model = 'multilingual-e5-large';

  const embeddings = await pc.inference.embed(
    model,
    chunks,
    { input_type: 'passage', truncate: 'END' }
  );
  console.log(embeddings) // 打印embeddings数组，检查其内容和结构

  const records = chunks.map((c, i) => ({
    id: Md5.hashStr(c),
    values: embeddings.data[i].values!,
    metadata: { text: c },
  }));
  return await pc.index('chat-bot').upsert(records);
};